﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace _File测试
{
    class Program
    {
        static void Main(string[] args)
        {
            //int page = 1;
            //int size = 20;
            //DataTable dt = new DataTable();

            //page = Convert.ToInt32(Console.ReadLine());
            //size = Convert.ToInt32(Console.ReadLine());

            //List<string> li = new List<string>() { "1", "2", "3", "4", "5", "6" };
            //List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            //for (int j = 1; j < 10; j++)
            //{
            //    Dictionary<string, object> row = new Dictionary<string, object>();
            //    for (int i = 1; i <= 9; i++)
            //    {
            //        row.Add("这是第" + j + "行，第" + i + "个元素", i);
            //    }
            //    rows.Add(row);
            //}

            //var li1 = rows.Skip((page - 1) * size).Take(size).ToList();
            //var li2 = rows.Skip(page - 1).ToList();
            //var li3 = rows.Take(size).ToList();


        }

        public static DataTable GetPageDataTable(DataTable dt2,int page, int size)
        {

            //测试栗子
            //DataTable dt2 = new DataTable("DT1");
            //dt2.Columns.Add("id", typeof(int));
            //dt2.Columns["id"].AutoIncrement = true;
            //dt2.Columns["id"].AutoIncrementSeed = 1;
            //dt2.Columns.Add("姓名", typeof(string));
            //dt2.Columns.Add("年龄", typeof(int));
            //dt2.Columns.Add("添加日期", typeof(DateTime));
            //DataRow DTrow = dt2.NewRow();
            //DTrow["姓名"] = "张三";
            //DTrow["年龄"] = 12;
            //DTrow["添加日期"] = DateTime.Now;

            //dt2.Rows.Add(DTrow);

            //DataRow DTrow1 = dt2.NewRow();
            //DTrow1["姓名"] = "李四";
            //DTrow1["年龄"] = 12;
            //DTrow1["添加日期"] = DateTime.Now;

            //dt2.Rows.Add(DTrow1);

            //DataRow DTrow2 = dt2.NewRow();
            //DTrow2["姓名"] = "王五";
            //DTrow2["年龄"] = 12;
            //DTrow2["添加日期"] = DateTime.Now;

            //dt2.Rows.Add(DTrow2);

            //DataRow DTrow3 = dt2.NewRow();
            //DTrow3["姓名"] = "马六";
            //DTrow3["年龄"] = 12;
            //DTrow3["添加日期"] = DateTime.Now;

            //dt2.Rows.Add(DTrow3);

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                Dictionary<string, object> row = new Dictionary<string, object>();
                for (int j = 0; j < dt2.Columns.Count; j++)
                {
                    row.Add(dt2.Columns[j].ColumnName, dt2.Rows[i][dt2.Columns[j].ColumnName]);
                }
                rows.Add(row);
            }

            rows = rows.Skip((page - 1) * size).Take(size).ToList();


            DataTable result = new DataTable("result");

            for (int i = 0; i < dt2.Columns.Count; i++)
            {
                result.Columns.Add(dt2.Columns[i].ColumnName);
            }

            for (int i = 0; i < rows.Count; i++)
            {
                DataRow dr = result.NewRow();
                foreach (var item in rows[i])
                {
                    dr[item.Key] = item.Value;
                }
                result.Rows.Add(dr);
            }

            return result;
        }
    }
}
